package com.lvs.zmwz.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lvs.zmwz.comm.DictionaryEnum;
import com.lvs.zmwz.entity.OrderList;
import com.lvs.zmwz.entity.Shop;
import com.lvs.zmwz.entity.ShopType;
import com.lvs.zmwz.service.IShopService;
import com.lvs.zmwz.service.IShopTypeService;
import com.lvs.zmwz.util.IdentityUtil;
import com.lvs.zmwz.util.QPage;
import com.lvs.zmwz.util.ResultView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2022-07-21
 */
@RestController
@RequestMapping("/api/admin/shop")
public class ShopController {

    @Autowired
    private IShopService iShopService;
    @Autowired
    private IShopTypeService iShopTypeService;

    /**
     * 商品列表
     * @return
     */
    @GetMapping("/list")
    public ResultView list(QPage qPage, Shop sysManagerSearch) {
        IPage<Shop> hjRoleList=this.iShopService.getListAll(qPage,sysManagerSearch);
        return ResultView.ok(hjRoleList);
    }


    /**
     * 添加商品
     * @return
     */
    @PostMapping("/add")
    public ResultView add(Shop shop) {
        shop.setShopId(IdentityUtil.identityId("SP"));
        shop.setCtime(new Date());
        shop.setIsDel(DictionaryEnum.IS_DEL_N.getCode());
        ShopType shopType=iShopTypeService.getById(shop.getTypeId());
        shop.setTypeName(shopType.getTypeName());
        boolean isOk=this.iShopService.save(shop);
        if(isOk){
            return ResultView.ok();
        }else{
            return ResultView.error("新增商品失败");
        }
    }


    /**
     * 商品详情
     * @return
     */
    @GetMapping("/getId")
    public ResultView getId(String shopId) {
        return ResultView.ok(this.iShopService.getById(shopId));
    }

    /**
     * 修改商品
     * @return
     */
    @PostMapping("/update")
    public ResultView update(Shop shop) {
        ShopType shopType=iShopTypeService.getById(shop.getTypeId());
        shop.setTypeName(shopType.getTypeName());
        boolean isOk=this.iShopService.updateById(shop);
        if(isOk){
            return ResultView.ok();
        }else{
            return ResultView.error("修改商品失败");
        }
    }


    /**
     * 删除商品
     * @return
     */
    @PostMapping("/delete")
    public ResultView delete(String shopId) {
        Shop shop=this.iShopService.getById(shopId);
        shop.setIsDel(DictionaryEnum.IS_DEL_Y.getCode());
        boolean isOk=this.iShopService.updateById(shop);
        if(isOk){
            return ResultView.ok();
        }else{
            return ResultView.error("删除失败");
        }
    }


}
